<?php

namespace app\beautiful\controller\admin\partner;

use app\BaseController;

use app\common\logic\DataLogic;

use app\beautiful\helper\payment\PartnerCommissionHelper;

use app\beautiful\model\partner\PartnerWithdrawModel;

use app\beautiful\model\PartnerModel;

use app\common\model\ShopModel;

use think\facade\Db;



class Withdraw extends BaseController{

    public function index()

    {

        $mobile=$this->request->param('mobile');

        $status=$this->request->param('status/d',0);

        $partner_id = $this->request->param('partner_id/d',0);

        $partner_type = $this->request->param('partner_type/d',0);



        $where=[

            ['shop_id','=',$this->request->shopId],

            ['status','=',$status]

        ];



        $partner_where=[

            ['shop_id','=',$this->request->shopId],

        ];

        if(!empty($mobile)) $partner_where[]=['mobile','=',$mobile];

        if($partner_type != 0) $where[]=['partner_type','=',$partner_type];

        if($partner_id==0){

            $partner_ids=PartnerModel::where($partner_where)->column('partner_id') ?? [];

            $where[]=['partner_id','in',$partner_ids];

        }else{

            $where[]=['partner_id','=',$partner_id];

        }





        $partner_withdraw_model= new PartnerWithdrawModel();

        $partner_withdraw_model=$partner_withdraw_model->with(['partner']);

        $order=['add_time'=>'desc'];

        $field='*';

        $append=['add_time_format','status_means'];

        $return= DataLogic::getDataList($partner_withdraw_model,$where,$order,$field,$this->request->limit,$this->request->page,$append);

        if($status==0){

            $return['to_do_num']=$return['total'];

        }else{

            $return['to_do_num']=PartnerWithdrawModel::where([

                ['shop_id','=',$this->request->shopId],

                ['status','=',0],

                ['partner_id','in',$partner_ids]

            ])->count() ?? 0;

        }

        foreach($return['list'] as $k=>$v){

            if($v['type'] == 1){

                $return['list'][$k]['type'] = '商家线下打款';

            } else {

                $return['list'][$k]['type'] = '系统自动打款';

            }

            if($v['partner_type'] == 1){

                $return['list'][$k]['partner_type'] = '股东合伙人';

            } else {

                $return['list'][$k]['partner_type'] = '会员合伙人';

            }

        }





        return $this->success($return);

    }



    public function agree()

    {

        $withdraw_id=$this->request->param('withdraw_id/d',0);

        $type=$this->request->param('type/d',0);

        if($withdraw_id==0) return $this->error('提现不存在');

        if(null === $withdraw = PartnerWithdrawModel::find($withdraw_id)) return $this->error('提现不存在');

        if($withdraw->shop_id != $this->request->shopId) return $this->error('提现不存在');

        if($withdraw->status != 0) return $this->error('这笔提现已经成功，请勿重复操作');

        if($type == '商家线下打款' || $type == 1){

            $withdraw->status = 1;

            $withdraw->save();

            return $this->success('提现成功');

        } else {

            Db::startTrans();

            try{

                $withdraw->status=1;



                $partner_commission_helper = new PartnerCommissionHelper($this->request->shopId);

                $res=$partner_commission_helper->sendPartnerCommission($withdraw->withdraw_id,$withdraw->open_id,$withdraw->money);

                if($res['return_code'] =='SUCCESS'){

                    if($res['result_code']=='SUCCESS'){

                        $withdraw->pay_time=strtotime($res['payment_time']);

                        $withdraw->save();

                        Db::commit();

                        return $this->success('提现成功');

                    }else{

                        Db::rollback();

                        return $this->error($res['err_code_des']);

                    }

                }else{

                    Db::rollback();

                    return $this->error('通信失败');

                }

            }catch(\Exception $e){

                Db::rollback();

                return $this->error($e->getMessage());

            }

        }



    }



    public function withdrawSetting(){



        $shopData = ShopModel::find($this->request->shopId);

        $data = [

            'mode' => $shopData->mode,

            'wechatnum' => $shopData->wechatnum,

        ];

        return $this->success(['detail'=>$data]);



    }



    public function saveWithdrawSetting(){



        $mode=$this->request->param('mode/d',0);

        $wechatnum=$this->request->param('wechatnum','');

        $shopData = ShopModel::find($this->request->shopId);

        $shopData->mode = $mode;

        $shopData->wechatnum = $wechatnum;

        $shopData->save();

        return $this->success();



    }



}

